package com.iking.toolkit.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.iking.toolkit.system.dao.DictionaryItemsMapper;
import com.iking.toolkit.system.dao.DictionaryTypeMapper;
import com.iking.toolkit.system.model.http.HttpDictionaryItemSelectReq;
import com.iking.toolkit.system.model.model.DictionaryItems;
import com.iking.toolkit.system.service.DictionaryItemsService;
import com.iking.toolkit.model.EmergencyUtil;
import com.iking.toolkit.model.Msg;
import com.iking.toolkit.model.PageRes;
import com.iking.toolkit.util.SysUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * dictionary_items（数据字典内容） ServiceImpl
 *
 * @author user
 * @since 2022-10-09
 */
@Slf4j
@Service
public class DictionaryItemsServiceImpl implements DictionaryItemsService {

    /**
     * 数据字典内容管理
     */
    @Autowired
    private DictionaryItemsMapper dictionaryItemsMapper;

    /**
     * 数据字典类型管理
     */
    @Autowired
    private DictionaryTypeMapper dictionaryTypeMapper;


    /**
     * @Description: 根据类型查询内容
     * @Param: [req]
     * @return: com.iking.common.model.Msg
     * @Author: wk
     * @Date: 7/2/2020 上午9:07
     */
    @Override
    public Msg<PageRes<DictionaryItems>> searchInstrumentsId(HttpDictionaryItemSelectReq req) {
        try {
            PageHelper.startPage(req.getCurrentPage(), req.getPageSize());
            Page<DictionaryItems> listDictionaryItems = dictionaryItemsMapper.searchInstrumentsId(req);
            PageRes<DictionaryItems> pageRes = EmergencyUtil.conversionPage(listDictionaryItems);
            return Msg.sucess(pageRes);
        } catch (Exception e) {
            log.error("根据数据字典类型查询内容异常,异常原因:" + e);
            return Msg.fail("根据数据字典类型查询内容异常");
        }
    }

    /**
     * @Description: 删除数据字典内容
     * @Param: [id]
     * @return: com.iking.common.model.Msg
     * @Author: wk
     * @Date: 7/2/2020 上午9:30
     */
    @Override
    public Msg deleteByPrimaryKey(String id) {
        try {
            DictionaryItems model = dictionaryItemsMapper.selectByPrimaryKey(id);
            if (model == null) {
                return Msg.fail("数据字典内容不存在");
            }
            if(model.getIsDelete()){
                return Msg.fail("数据字典内容已删除！");
            }
            model.setIsDelete(true);
            int count = dictionaryItemsMapper.updateByPrimaryKeySelective(model);
            if (count < 1) {
                return Msg.fail("删除失败");
            }
            return Msg.sucess("删除成功");
        } catch (Exception e) {
            log.error("删除数据字典内容异常,异常原因:" + e);
            return Msg.fail("删除数据字典内容异常");
        }
    }

    /**
     * @Description: 修改数据字典内容
     * @Param: [req]
     * @return: com.iking.common.model.Msg
     * @Author: wk
     * @Date: 7/2/2020 上午9:28
     */
    @Override
    public Msg update(DictionaryItems req) {
        try {
            DictionaryItems model = dictionaryItemsMapper.selectByPrimaryKey(req.getDictionaryItemsId());
            if (model == null) {
                return Msg.fail("数据字典内容不存在");
            }
            if(model.getIsDelete()){
                return Msg.fail("数据字典内容已删除！");
            }
            if (req.getDictionaryTypeId() != null
                    && (dictionaryTypeMapper.selectByPrimaryKey(req.getDictionaryTypeId()) == null || dictionaryTypeMapper.selectByPrimaryKey(req.getDictionaryTypeId()).getIsDelete())) {
                return Msg.fail("数据字典类型不存在");
            }
            List<DictionaryItems> listPymUpdate = null;
            List<DictionaryItems> listNameUpdate = null;
            // 修改了拼音码后校验其存在性
            if (req.getItemPym() != null &&
                    !req.getItemPym().equals(model.getItemPym())) {
                listPymUpdate = dictionaryItemsMapper.searchByPymUpdate(req);
            }
            // 修改了名称后校验其存在性
            if (req.getItemName() != null &&
                    !req.getItemName().equals(model.getItemName())) {
                listNameUpdate = dictionaryItemsMapper.searchByNameUpdate(req);
            }
            // 拼音码已存在
            if (listPymUpdate != null && listPymUpdate.size() > 0) {
                return Msg.fail("参数" + req.getItemPym() + "已存在！");
            }
            // 名称已存在
            if (listNameUpdate != null && listNameUpdate.size() > 0) {
                return Msg.fail("字典内容" + req.getItemName() + "已存在！");
            }
            model.setItemPym(req.getItemPym());
            model.setDictionaryTypeId(req.getDictionaryTypeId());
            model.setItemName(req.getItemName());
            model.setItemValue(req.getItemValue());
            model.setDescription(req.getDescription());
            // 排序禁止修改
            model.setSort(null);
            model.setIsDelete(req.getIsDelete());
            model.setIsEnable(req.getIsEnable());
            model.setUpdateTime(new Date());
            int count = dictionaryItemsMapper.updateByPrimaryKeySelective(model);
            if (count < 1) {
                return Msg.fail("修改数据字典内容失败");
            }
            return Msg.sucess("修改成功");
        } catch (Exception e) {
            log.error("修改数据字典内容异常,异常信息:" + e);
            return Msg.fail("修改数据字典内容异常");
        }
    }

    /**
     * @Description: 新增数据字典内容
     * @Param: [req]
     * @return: com.iking.common.model.Msg
     * @Author: wk
     * @Date: 7/2/2020 上午9:27
     */
    @Override
    public Msg insert(DictionaryItems req) {
        try {
            List<DictionaryItems> listPymInsert = dictionaryItemsMapper.searchByPymInsert(req);
            List<DictionaryItems> listNameInsert = dictionaryItemsMapper.searchByNameInsert(req);
            if (listPymInsert != null && listPymInsert.size() > 0) {
                return Msg.fail("参数已存在");
            }
            if (listNameInsert != null && listNameInsert.size() > 0) {
                return Msg.fail("字典内容已存在");
            }
            req.setDictionaryItemsId(SysUtil.getUUID());
            req.setCreateTime(new Date());
            req.setUpdateTime(new Date());
            req.setIsEnable(true);
            req.setIsDelete(false);
            int sort = dictionaryItemsMapper.MaxSort();
            sort++;
            // 新增时排序+1
            req.setSort(sort);
            int count = dictionaryItemsMapper.insert(req);
            if (count < 1) {
                return Msg.fail("新增数据字典内容失败");
            }
            return Msg.sucess("新增成功");
        } catch (Exception e) {
            log.error("新增数据字典内容异常,异常信息:" + e);
            return Msg.fail("新增数据字典内容异常");
        }
    }


    /**
     * @Description: 按参数查询 Msg
     * @Param: [pym]
     * @return: com.iking.common.model.Msg
     * @Author: wk
     * @Date: 7/2/2020 上午9:25
     */
    @Override
    public Msg searchInstrumentsPym(String pym) {
        try {
            List<DictionaryItems> listDictionaryItems = dictionaryItemsMapper.searchInstrumentsPym(pym);
            return Msg.sucess(listDictionaryItems);
        } catch (Exception e) {
            log.error("按参数查询异常,异常信息:" + e);
            return Msg.fail("按参数查询异常");
        }
    }

    /**
     * @Description: 根据数据字典类型拼音码和数据字典内容拼音码查询数据字典内容
     * @Param: [valuepym, typepym]
     * @return: com.iking.common.model.Msg
     * @Author: wk
     * @Date: 7/2/2020 上午9:31
     */
    @Override
    public Msg<DictionaryItems> selectByTypeIdAndPym(String valuepym, String typepym) {
        try {
            DictionaryItems dictionaryItems = dictionaryItemsMapper.selectByTypeIdAndPym(valuepym, typepym);
            return Msg.sucess(dictionaryItems);
        } catch (Exception e) {
            log.error(" 根据字典类型和字典内容拼音码查询异常,异常信息:" + e);
            return Msg.fail("根据字典类型和字典内容拼音码查询异常");
        }
    }

    /**
     * @Description: 根据数据字典类型拼音码查询数据字典内容
     * @Param: [typepym]
     * @return: com.iking.common.model.Msg
     * @Author: wk
     * @Date: 7/2/2020 上午9:32
     */
    @Override
    public Msg selectByTypePym(String typepym) {
        try {
            List<DictionaryItems> items = dictionaryItemsMapper.selectByTypePym(typepym);
            return Msg.sucess(items);
        } catch (Exception e) {
            log.error("根据名称和类型id查询字典内容集合异常,异常信息:" + e);
            return Msg.fail("根据名称和类型id查询字典内容集合异常");
        }

    }

}
